<?php


namespace api\modules\v1\controllers\system;

use Yii;
use common\helpers\CommonHelper;
use api\controllers\OnAuthController;
use api\modules\v1\forms\clinic\RelationKefuForm;

class BusinessController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/system/business/list 业务设置列表
     * @apiName list
     * @apiGroup SystemBusiness
     *
     * @apiParam {Number} page                              页码
     * @apiParam {Number} pageSize                          每页显示数量
     *
     * @apiSuccess {Object}  data                           返回数据
     * @apiSuccess {Number}  data.page                      当前页码
     * @apiSuccess {Number}  data.pageSize                  每页显示数
     * @apiSuccess {Number}  data.total                     总数
     * @apiSuccess {Array}   data.list                      患者列表
     * @apiSuccess {Number}  data.list.id                   客服ID
     * @apiSuccess {String}  data.list.realname             姓名
     * @apiSuccess {Array}   data.list.clinic_name_arr      绑定诊室数组
     *
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = $this->projectId;

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 查询结果
        $data = Yii::$app->services->clinicRelationKefu->getListPage($params, (int)$page, (int)$pageSize);

        // 格式化列表数据
        $data['list'] = Yii::$app->services->clinicRelationKefu->getListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {get} /v1/system/business/info 详情
     * @apiName info
     * @apiGroup SystemBusiness
     *
     * @apiParam {Number} kefu_id                               客服ID
     *
     * @apiSuccess {Object}  data                               返回数据
     * @apiSuccess {Number}  data.id                            客服ID
     * @apiSuccess {String}  data.realname                      姓名
     * @apiSuccess {Array}   data.clinic_id_arr                 绑定诊室IDs【数组】
     * @apiSuccess {Array}   data.condition                     筛选条件
     * @apiSuccess {Array}   data.condition.clinic_list         医助归属人
     * @apiSuccess {Number}  data.condition.clinic_list.id      ID
     * @apiSuccess {String}  data.condition.clinic_list.name    名称
     *
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $params['kefu_id'] = $params['kefu_id'] ?? 0;

        // 客服信息
        $member = Yii::$app->services->member->findById($params['kefu_id']);

        // 查询结果
        $list = Yii::$app->services->clinicRelationKefu->findList([
            'kefu_id' => $params['kefu_id'],
            'project_id' => $this->projectId,
        ]);

        // 获取筛选条件
        $params = [
            'get_clinic' => 1,
        ];
        $condition = Yii::$app->services->patientUser->getConditionList($params, $this->projectId);

        return [
            'id' => $member->id,
            'realname' => $member->realname,
            'clinic_id_arr' => array_column($list, 'clinic_id'),
            'condition' => $condition,
        ];
    }

    /**
     * @api {post} /v1/system/business/update 编辑客服诊室
     * @apiName update
     * @apiGroup SystemBusiness
     *
     * @apiParam {Number} kefu_id                               客服ID
     * @apiParam {String} clinic_ids                            诊室IDs【多个用','拼接】
     *
     * @apiSuccess {Object}  data                               返回数据
     * @apiSuccess {Number}  data.kefu_id                       客服ID
     *
     */
    public function actionUpdateKefu()
    {
        $params = Yii::$app->request->post();
        $params['project_id'] = $this->projectId;
        $params['clinic_ids'] = $params['clinic_ids'] ?? '';

        $model = new RelationKefuForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        return Yii::$app->services->clinicRelationKefu->edit($model);

    }
}
